// var imageToSlices = require('image-to-slices');
import { v4 as uuidv4 } from 'uuid';

import { Jimp, JimpMime } from "jimp";
import * as resize from "@jimp/plugin-resize";
import path from "path"
resize.ResizeStrategy.BICUBIC
/**
 * 
 * @param imgAlastNative 
 * @param nativeUrl 
 * @param rect 
 */
export  async function spiltImg(imgAlastNative:string,nativeUrl:string,rect:any,exName:string,reSizeImgSize?:{
    w:number,
    h:number
}){

    
    // console.warn(s[])
    // console.warn("spiltImg===>",imgAlastNative,nativeUrl,rect,exName)
    let rg = /\/spriteFrame/
    let m = nativeUrl.replace(rg,"")
    let lastIndex=  m.lastIndexOf("/")
    m = m.slice(0,lastIndex+1)
    // console.warn(m)
    m = `${m}${rect.name}`
    nativeUrl = m
    // console.warn(Jimp.read)
    

    let image =  await Jimp.read(imgAlastNative)

    if(rect.rotated){
        await image.crop({
            x:rect.rect.x,
            y:rect.rect.y,
            w:rect.rect.height,
            h:rect.rect.width
        })
        image.rotate(90)
    }else{
        await image.crop({
            x:rect.rect.x,
            y:rect.rect.y,
            w:rect.rect.width,
            h:rect.rect.height
        })
    }

    // let reSizeImgSize = {
    //     w:1024,
    //     h:1024
    // }
    //@ts-ignore
    await image.write(`${nativeUrl}${exName}`)
    if(reSizeImgSize){
        
        let jimNew = await Jimp.read("./xx.png")
       await  jimNew.resize({
            w:reSizeImgSize.w,
            h:reSizeImgSize.h,
        })
        
    //    await  jimNew.composite(image,reSizeImgSize.w/2 -(rect.rect.width/2),reSizeImgSize.h/2-(rect.rect.height/2))
       await  jimNew.composite(image,0,reSizeImgSize.h/2-(rect.rect.height/2))
        //@ts-ignore
       await  jimNew.write(`${nativeUrl}${exName}`)
    }
    return `${nativeUrl}${exName}`
    
}
export async function resizeImgSize(navUrl:string,width:number,height:number){
    let image =  await Jimp.read(navUrl)
    image
    //@ts-ignore
    await image.write(`${navUrl}`)
    
}
export function stripImgUUid(text:string){
    let s = text.split("\n")
    for(let i = 0 ;i<s.length;i++){
        let item = s[i]
        let indexofUUid = item.indexOf("<key>spriteFrameUuid</key>")
        if(indexofUUid != -1){
            let uid = s[i+1]
            // console.warn(uid)
            let rg = /(?<=\<string\>).*(?=\<\/string\>)/
            let r = uid.match(rg)
            console.warn("xxx====>",r[0])
            // if(r[0])
            return r[0]
        }
    }
    return ""
}
export function replcaeUUid(text:string,newUuid:string){
    let s = text.split("\n")
    let newStr = ""
    for(let i = 0 ;i<s.length;i++){
        let item = s[i]
        let indexofUUid = item.indexOf("<key>spriteFrameUuid</key>")
        if(indexofUUid != -1){
            let v = s[i+1]
            s[i+1] =  `${v.slice(0,indexofUUid)}<key>${newUuid}</key>`
       
       
            newStr+=  "<key>blendFuncDestination</key>\n"
            newStr+=  "<integer>1</integer>\n"
            newStr+=  "<key>blendFuncSource</key>\n"
            newStr+=  "<integer>770</integer>\n"
        }
        newStr+=`${item}\n`
    }
    return newStr
}
/**
 * 生成图片的uuid作为粒子系统
 * @param imgPath 
 * @param uuid 
 */
export function GenerImgUUidMeat(uuid1:string,uuid2:string,rect:any){

    let spfName = rect.name
    let obj = 
    {
        "ver": "2.3.7",
        "uuid":uuid1,
        "importer": "texture",
        "type": "sprite",
        "wrapMode": "clamp",
        "filterMode": "bilinear",
        "premultiplyAlpha": false,
        "genMipmaps": false,
        "packable": true,
        "width": 88,
        "height": 87,
        "platformSettings": {},
        "subMetas": {
        }
      }
      let spfObj =  {
        "ver": "1.0.6",
        "uuid": uuid2,
        "importer": "sprite-frame",
        "rawTextureUuid": uuid1,
        "trimType": "auto",
        "trimThreshold": 1,
        "rotated": false,
        "offsetX": 0,
        "offsetY": 0,
        "trimX": 0,
        "trimY": 0,
        "width": rect.rect.width,
        "height": rect.rect.height,
        "rawWidth": rect.rect.width,
        "rawHeight": rect.rect.height,
        "borderTop": 0,
        "borderBottom": 0,
        "borderLeft": 0,
        "borderRight": 0,
        "subMetas": {}
      }
      //@ts-ignore
      obj["subMetas"][spfName] = spfObj
      return JSON.stringify(obj,null,1)
}
const pathUUid:{[key:string]:string[]} = {
    
}

/**
 * 给精灵生成uuid
 */
export function getUuidForPath(filePath:string,len:number){ 
    filePath = path.normalize(filePath)
    console.warn(filePath)
    if(pathUUid[filePath]){
        return pathUUid[filePath]
    }
    let uuidds = pathUUid[filePath] ?? []
    for(let i = 0;i<len;i++){
        uuidds.push(uuidv4())
    }
    pathUUid[filePath] = uuidds
    return uuidds    
}
const  spilt =  async () =>{
}

spilt()